# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=ruby-mathematical
_gemname=${pkgname#ruby-}
pkgver=1.6.13
pkgrel=0
pkgdesc="Convert mathematical equations to SVGs, PNGs, or MathML"
url="https://www.gjtorikian.com/mathematical/"
arch="all !aarch64 !armv7 !mips !mips64 !ppc64le !s390x !x86" # Limited by lasem and ruby-enum
license="MIT"
depends="ruby ruby-enum"
makedepends="cmake lasem-dev mtex2mml-dev ruby-dev"
checkdepends="mtex2mml-fixtures ruby-io-console ruby-irb ruby-json
	ruby-minitest ruby-nokogiri ruby-rake"
options="!check" # TODO: tests fails with ruby 2.7.
source="$pkgname-$pkgver.tar.gz::https://github.com/gjtorikian/$_gemname/archive/v$pkgver.tar.gz
	use-system-mtex2mml-fixtures.patch"
builddir="$srcdir/$_gemname-$pkgver"

prepare() {
	default_prepare

	# Don't load bundler, it messes with GEM_PATH.
	sed -i "/^require\s*[\"']bundler\/setup/d" test/test_helper.rb

	# Some tests in this file are unstable (randomly fails).
	# https://github.com/gjtorikian/mathematical/issues/73
	rm test/mathematical/delimiters_test.rb
}

build() {
	gem build $_gemname.gemspec
	gem install --local \
		--install-dir dist \
		--ignore-dependencies \
		--no-document \
		--verbose \
		$_gemname -- \
			--use-system-lasem \
			--use-system-mtex2MML
}

check() {
	local dev_deps

	# Install missing development dependencies needed and used only for running
	# tests. We can't install them easily with bundler, because there's
	# no way how to force it to NOT update already satisifed dependencies.
	# We can't install them directly with gem either, because there's no
	# way how to force it to instally only dependencies, not the gem itself.
	dev_deps=$(gem install --explain --development --minimal-deps $_gemname-$pkgver.gem \
		| head -n -1 | sed -En 's/-([^-]+)$/:\1/p')
	gem install --instal-dir=.gem --conservative $dev_deps

	GEM_PATH='dist:.gem' ruby -S rake test
}

package() {
	local gemdir="$pkgdir/$(ruby -e 'puts Gem.default_dir')"
	cd "$builddir"/dist

	mkdir -p "$gemdir"
	cp -r extensions gems specifications "$gemdir"/

	# Remove unnecessary files and rubbish...
	find "$gemdir"/extensions/ -name mkmf.log -delete
	cd "$gemdir"/gems/$_gemname-$pkgver
	rm -rf -- ext/ test/ Rakefile *.md *.txt
}

sha512sums="16ec5e4da958e0851237fc3d1f1b26e82e14e0ce48a3955eed0e8630d9781652b192559667448a44662c94ac5844658cba39712b734a90c30bb51098ed2bf39c  ruby-mathematical-1.6.13.tar.gz
35c691b2006e6c0bac26ebb0adac5ffa99fbc37100c94d5978508b91b3df84544e4fc8fa38b5de01ae0fcc644c1eeb97b6129f59b4488b467c1ded468277c400  use-system-mtex2mml-fixtures.patch"
